
require 'iconv'

def gb2312_to_utf8(text)
  utf8_text = text
  begin
    utf8_text = Iconv.conv('UTF-8', 'GB2312', text)    
  rescue Iconv::IllegalSequence => e
  end
  utf8_text
end

def utf8_to_gb2312(text)
  gb2312_text = text
  begin
    gb2312_text = Iconv.conv('GB2312', 'UTF-8', text)
  rescue Iconv::IllegalSequence => e
    puts 'Can not convert to GB2312: ' + e.class.name
  rescue Iconv::InvalidCharacter => e
    puts 'Can not convert to GB2312: ' + e.class.name
  end
  gb2312_text
end


text = ""
open(ARGV[0]) do |f|
  text = f.read
end

utext = gb2312_to_utf8 text

open(ARGV[1], 'w') do |f|
  f.print utext
end

f = open(ARGV[1]) 
utext = f.read
f.close

gtext = utf8_to_gb2312 utext
f = open(ARGV[2], 'w')
f.print gtext
f.close



